package com.example.demo.controller;

import com.example.demo.DBmake.PostgreDB;
import com.example.demo.Utils.TokenUtil;
import com.example.demo.api.CommonResult;
import com.example.demo.entity.MessInfo;
import com.example.demo.entity.UpdInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class MessController {
    @RequestMapping(value = "/message",method = RequestMethod.POST)
    public CommonResult login(@RequestBody MessInfo messInfo){

        if(messInfo.getToken()=="")  return CommonResult.unauthorized("登录过期");

        int myUid = Integer.parseInt(messInfo.getMyUid());
        int toUid = Integer.parseInt(messInfo.getToUid());
        String token = messInfo.getToken();

        if(!TokenUtil.verify(token)) return CommonResult.unauthorized("登录过期");

        PostgreDB Target = new PostgreDB();

        String sql = "INSERT INTO public.usercon VALUES(DEFAULT,"+toUid+","+myUid+",'"+messInfo.getInputText()+"',1,now())";

        if(!Target.updateItem(sql)) return CommonResult.ErrorDateBase();

        token = TokenUtil.sign(myUid);
        return CommonResult.success(myUid,token);
    }
}
